#include <ptgui/ptgui.h>
#include <ptgui/image.h>
#include <ptgui/system.h>

#define HDC_MAGIC_LEN   	4

struct ptgui_image_hdc
{
	rt_bool_t					is_loaded;

	struct ptgui_filerw			*filerw;
	struct ptgui_graphic_driver	*hw_driver;

	/* hdc image information */
	rt_uint16_t					byte_per_pixel;
	rt_uint16_t					pitch;

	rt_size_t					pixel_offset;
	rt_uint8_t					*pixels;
};

static rt_bool_t ptgui_image_hdc_check(struct ptgui_filerw *file);
static rt_bool_t ptgui_image_hdc_load(struct ptgui_image *image, struct ptgui_filerw *file, rt_bool_t load);
static void ptgui_image_hdc_unload(struct ptgui_image *image);
static void ptgui_image_hdc_blit(struct ptgui_image *image, struct ptgui_dc *dc, struct ptgui_rect *rect);

struct ptgui_image_engine ptgui_image_hdc_engine =
{
	"hdc",
	{ RT_NULL },
	ptgui_image_hdc_check,
	ptgui_image_hdc_load,
	ptgui_image_hdc_unload,
	ptgui_image_hdc_blit
};

static rt_bool_t ptgui_image_hdc_check(struct ptgui_filerw *file)
{
	int start;
	rt_bool_t is_HDC;
	rt_uint8_t magic[4];

	if (!file) {
		return 0;
	}

	start = ptgui_filerw_tell(file);

	/* move to the beginning of file */
	ptgui_filerw_seek(file, 0, PTGUI_FILE_SEEK_SET);

	is_HDC = RT_FALSE;
	if (ptgui_filerw_read(file, magic, 1, sizeof(magic)) == sizeof(magic)) {
		if (magic[0] == 'H' && magic[1] == 'D' && magic[2] == 'C' && magic[3] == '\0') {
			is_HDC = RT_TRUE;
		}
	}
	ptgui_filerw_seek(file, start, PTGUI_FILE_SEEK_SET);

	return(is_HDC);
}

static rt_bool_t ptgui_image_hdc_load(struct ptgui_image *image, struct ptgui_filerw *file, rt_bool_t load)
{
	rt_uint32_t header[5];
	struct ptgui_image_hdc *hdc;

	hdc = (struct ptgui_image_hdc *)ptgui_malloc(sizeof(struct ptgui_image_hdc));
	if (hdc == RT_NULL) {
		return RT_FALSE;
	}

	hdc->hw_driver = ptgui_graphic_driver_get_default();
	if (hdc->hw_driver == RT_NULL) {
		ptgui_free(hdc); return RT_FALSE;
	}

	ptgui_filerw_read(file, (char*)&header, 1, sizeof(header));

	/* set image information */
	image->w = (rt_uint16_t)header[1]; image->h = (rt_uint16_t)header[2];
	image->engine = &ptgui_image_hdc_engine;
	image->data = hdc;
	hdc->filerw = file;
	hdc->byte_per_pixel = hdc->hw_driver->byte_per_pixel;
	hdc->pitch = image->w * hdc->byte_per_pixel;
	hdc->pixel_offset = ptgui_filerw_tell(file);

	if (load == RT_TRUE) {
		/* load all pixels */
		hdc->pixels = ptgui_malloc(image->h * hdc->pitch);
		if (hdc->pixels == RT_NULL) {
			/* release data */
			ptgui_free(hdc);
			return RT_FALSE;
		}

		ptgui_filerw_read(hdc->filerw, hdc->pixels, 1, image->h * hdc->pitch);
		ptgui_filerw_close(hdc->filerw);
		hdc->filerw = RT_NULL;
		hdc->pixel_offset = 0;
	}
	else {
		hdc->pixels = RT_NULL;
	}

	return RT_TRUE;
}

static void ptgui_image_hdc_unload(struct ptgui_image *image)
{
	struct ptgui_image_hdc *hdc;

	if (image != RT_NULL) {
		hdc = (struct ptgui_image_hdc *)image->data;

		if (hdc->pixels != RT_NULL) {
			ptgui_free(hdc->pixels);
		}
		if (hdc->filerw != RT_NULL) {
			ptgui_filerw_close(hdc->filerw);
			hdc->filerw = RT_NULL;
		}

		/* release data */
		ptgui_free(hdc);
	}
}

static void ptgui_image_hdc_raw_hline(struct ptgui_dc_hw *dc, rt_uint8_t *raw_ptr, int x1, int x2, int y)
{
	register rt_base_t bpp;
	ptgui_rect_t *prect;

	/* convert logic to device */
	x1 = x1 + dc->owner->extent.x1;
	x2 = x2 + dc->owner->extent.x1;
	y = y + dc->owner->extent.y1;

	bpp = dc->device->byte_per_pixel;
	prect = &(dc->owner->extent);

	/* calculate hline intersect */
	if (prect->y1 > y || prect->y2 <= y) {
		return;
	}
	if (prect->x2 <= x1 || prect->x1 > x2) {
		return;
	}

	if (prect->x1 > x1) {
		x1 = prect->x1;
	}
	if (prect->x2 < x2) {
		x2 = prect->x2;
	}

	/* draw raw hline */
	dc->device->draw_raw_hline(raw_ptr, x1, x2, y);
}

static void ptgui_image_hdc_blit(struct ptgui_image *image, struct ptgui_dc *dc, struct ptgui_rect *dst_rect)
{
	rt_uint16_t y,
				w,
				h;
	struct ptgui_image_hdc *hdc;

	RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL);

	/* this dc is not visible */
	if (dc->get_visible(dc) != RT_TRUE) {
		return;
	}

	hdc = (struct ptgui_image_hdc *)image->data;
	RT_ASSERT(hdc != RT_NULL);

	if (dc->type != PTGUI_DC_HW) {
		return;
	}

	/* the minimum rect */
	if (image->w < ptgui_rect_width(*dst_rect)) {
		w = image->w;
	}
	else {
		w = ptgui_rect_width(*dst_rect);
	}
	if (image->h < ptgui_rect_height(*dst_rect)) {
		h = image->h;
	}
	else {
		h = ptgui_rect_height(*dst_rect);
	}

	if (hdc->pixels != RT_NULL) {
		rt_uint8_t *ptr;

		/* get pixel pointer */
		ptr = hdc->pixels;

		for (y = 0; y < h; y ++) {
			ptgui_image_hdc_raw_hline((struct ptgui_dc_hw *)dc, ptr, dst_rect->x1, dst_rect->x1 + w, dst_rect->y1 + y);
			ptr += hdc->pitch;
		}
	}
	else {
		rt_uint8_t *ptr;
		ptr = ptgui_malloc(hdc->pitch);
		if (ptr == RT_NULL) {
			return;
		} /* no memory */

		/* seek to the begin of pixel data */
		ptgui_filerw_seek(hdc->filerw, hdc->pixel_offset, PTGUI_FILE_SEEK_SET);

		for (y = 0; y < h; y ++) {
			/* read pixel data */
			if (ptgui_filerw_read(hdc->filerw, ptr, 1, hdc->pitch) != hdc->pitch) {
				break;
			} /* read data failed */

			ptgui_image_hdc_raw_hline((struct ptgui_dc_hw *)dc, ptr, dst_rect->x1, dst_rect->x1 + w, dst_rect->y1 + y);
		}

		ptgui_free(ptr);
	}
}

void ptgui_image_hdc_init()
{
	/* register hdc on image system */
	ptgui_image_register_engine(&ptgui_image_hdc_engine);
}
